import { defineStore } from 'pinia'
import i18n from '../../assets/i18n'
import Storage from '@/assets/js/storage'

export default defineStore('common', {
  state: () => ({
    locale: Storage.get('common')?.locale ?? i18n.global.locale.value,
    messages: i18n.global.messages.value,
    isCollapse: false,
    receivedActionMessages: [],
  }),
  actions: {
    setLocale(lang) {
      this.locale = lang
      i18n.global.locale.value = lang
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    setWsMsg(msg) {
      if (msg && msg != null) {
        this.receivedActionMessages.push(msg)
      }
    },
  },
  getters: {
    getLocale(state) {
      return state.locale
    },
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'common',
        storage: localStorage,
      },
    ],
  },
})
